package com.marketingexpert.service.streammgnt.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;

import com.marketingexpert.dao.StreamDao;
import com.marketingexpert.domain.Keyword;
import com.marketingexpert.domain.Panel;
import com.marketingexpert.domain.Tab;
import com.marketingexpert.exception.MarketingExpertException;
import com.marketingexpert.service.streammgnt.StreamService;
import com.marketingexpert.util.Resources;

public class StreamServiceImpl implements StreamService {

	private static Logger logger = Logger.getLogger(StreamServiceImpl.class);

	private StreamDao streamDao = null;

	public int saveTab(Tab tab) throws MarketingExpertException {

		try {
			int tabId = getStreamDao().saveTab(tab);
			return tabId;
		} catch (DataAccessException e) {
			logger.error(Resources.getString("Stream.insertTabInfoFailed"), e);
			throw new MarketingExpertException(Resources
					.getString("Stream.insertTabInfoFailed"), e);
		}
	}

	public StreamDao getStreamDao() {
		return streamDao;
	}

	public void setStreamDao(StreamDao streamDao) {
		this.streamDao = streamDao;
	}

	public int queryTabCount(String userId) throws MarketingExpertException {
		try {
			int tabCount = getStreamDao().queryTabCount(userId);
			return tabCount;
		} catch (DataAccessException e) {
			logger.warn(Resources.getString("Stream.queryTabCountFailed"), e);
			throw new MarketingExpertException(Resources
					.getString("Stream.queryTabCountFailed"), e);
		}
	}

	public int savePanel(Panel panel) throws MarketingExpertException {
		try {
			int paenlId = getStreamDao().savePanel(panel);
			return paenlId;
		} catch (DataAccessException e) {
			logger.warn(Resources
					.getString("Stream.insertTabInfoFailed"), e);
			throw new MarketingExpertException(Resources
					.getString("Stream.insertTabInfoFailed"), e);
		}
	}

	public Panel queryPanelByTabId(int tabId) throws MarketingExpertException {
		try {
			Panel panel = getStreamDao().queryPanelByTabId(tabId);
			return panel;
		} catch (DataAccessException e) {
			logger.warn(Resources
					.getString("Stream.queryPanelByUserId"), e);
			throw new MarketingExpertException(Resources
					.getString("Stream.queryPanelByUserId"), e);
		}
	}

	public Keyword queryKeywordsByPanelId(int panelId)
			throws MarketingExpertException {
		try {
			Keyword keywords = getStreamDao().queryKeywordsByPanelId(panelId);
			return keywords;
		} catch (DataAccessException e) {
			logger.warn(Resources
					.getString("Stream.queryKeywordsByPanelIdFailed"), e);
			throw new MarketingExpertException(Resources
					.getString("Stream.queryKeywordsByPanelIdFailed"), e);
		}
	}

	public List<Keyword> insertKeywords(List<Keyword> keywords, int panelId)
			throws MarketingExpertException {
		try {
			keywords = getStreamDao().insertKeywords(keywords, panelId);
			return keywords;
		} catch (DataAccessException e) {
			logger.error(Resources.getString("Stream.queryKeywordsByPanelIdFailed",new String[]{"StreamServiceImpl,Line 98!"}), e);
			throw new MarketingExpertException(Resources.getString("Stream.queryKeywordsByPanelIdFailed"), e);
		}
	}

	public List<Tab> queryTabByUserId(String parentUserId)
			throws MarketingExpertException {
		try {
			return getStreamDao().insertKeywords(parentUserId);
		} catch (DataAccessException e) {
			logger.warn(Resources.getString("StreamServiceImpl.0"), e);
			throw new MarketingExpertException(Resources
					.getString("Stream.queryKeywordsByPanelIdFailed"), e);
		}
	}

	public int queryPanelCountsByTabId(int tabId)throws MarketingExpertException  {
		try {
			return getStreamDao().queryPanelCountsByTabId(tabId);
		} catch (DataAccessException e) {
			logger.warn(Resources.getString("StreamServiceImpl.0"), e);
			throw new MarketingExpertException(Resources
					.getString("Stream.queryKeywordsByPanelIdFailed"), e);
		}
	}

	public List<Panel> savePanelBatch(List<Panel> panels)
			throws MarketingExpertException {
		try {
			return getStreamDao().savePanelBatch(panels);
		} catch (DataAccessException e) {
			logger.warn(Resources.getString("StreamServiceImpl.0"), e);
			throw new MarketingExpertException(Resources
					.getString("Stream.queryKeywordsByPanelIdFailed"), e);
		}
	}

	public Tab queryTabById(int tabId) throws MarketingExpertException {
		try {
			return getStreamDao().queryTabById(tabId);
		} catch (DataAccessException e) {
			logger.warn(Resources.getString("StreamServiceImpl.0"), e);
			throw new MarketingExpertException(Resources
					.getString("Stream.queryKeywordsByPanelIdFailed"), e);
		}
	}

	public void deleteTabByUserId(String userId)
			throws MarketingExpertException {
		try {
			getStreamDao().deleteTabByUserId(userId);
		} catch (DataAccessException e) {
			logger.warn(Resources.getString("StreamServiceImpl.0"), e);
			throw new MarketingExpertException(Resources
					.getString("Stream.queryKeywordsByPanelIdFailed"), e);
		}
		
	}

	public void deletePanle(int panelId) throws MarketingExpertException{
		try {
			getStreamDao().deletePanle(panelId);
		} catch (DataAccessException e) {
			logger.warn(Resources.getString("StreamServiceImpl.0"), e);
			throw new MarketingExpertException(Resources
					.getString("Stream.queryKeywordsByPanelIdFailed"), e);
		}		
	}

	public void deleteTabById(int tabId) throws MarketingExpertException {
		try {
			getStreamDao().deleteTabById(tabId);
		} catch (DataAccessException e) {
			logger.warn(Resources.getString("StreamServiceImpl.0"), e);
			throw new MarketingExpertException(Resources
					.getString("Stream.deleteTabByIdFailed"), e);
		}
	}
	
	
}
